package com.ejia.ui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JSpinner;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import com.ejia.pojo.fytype;
import com.ejia.pojo.money;
import com.ejia.pojo.notice;
import com.ejia.pojo.user;
import com.ejia.server.MoneyService;
import com.ejia.server.MtypeService;
import com.ejia.server.UserService;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JComboBox;
import javax.swing.border.TitledBorder;
import java.awt.Color;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;
import javax.swing.SpinnerListModel;
import javax.swing.SpinnerDateModel;
import java.util.Date;
import java.util.ArrayList;
import java.util.Calendar;
import javax.swing.ImageIcon;
import java.awt.Font;
import java.awt.List;
import java.awt.event.ActionListener;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.awt.event.ActionEvent;
import javax.swing.DefaultComboBoxModel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Toolkit;

public class FYGL extends JFrame {

	private JPanel contentPane;
	private JTextField txtName;
	private static JTable table;
	private static JTable table_1;
	private static JTextField txtnumber;
	private static ArrayList<user> users;
	private static ArrayList<money> moneys;
	private static ArrayList<fytype> fytypes;
	private JTextField txtstat;
	private JTextField txtend;
	private double jsjg=0;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					FYGL frame = new FYGL();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public static void dataBind(String name) {
		if (name.equals("")) {
			users=UserService.YZZHXXfind();
		}else {
			users=UserService.YZZHXX(name);
		}
		Object[][] data =new Object[users.size()][];
		for (int i = 0; i < data.length; i++) {
			data[i]=new Object[] {
					users.get(i).getHid().getHname(),users.get(i).getRealname()
			};
		}
		table.setModel(new DefaultTableModel(
				data,
				new String[] {
					"\u95E8\u724C\u53F7", "\u4E1A\u4E3B"
				}
			));
		table.getTableHeader().setFont(new Font("微软雅黑", 0, 20));
	}
	
	
	
	public static void dataBind1(String name) {
		if (name.equals("")) {
			moneys=MoneyService.WYJFMX();
		}else {
			moneys=MoneyService.WYJFMXfind(name);
		}
		Object[][] data =new Object[moneys.size()][];
		for (int i = 0; i < data.length; i++) {
			if (moneys.get(i).getStatus()==0) {
				data[i]=new Object[] {
					moneys.get(i).getMid(),
					moneys.get(i).getUserid().getRealname(),
					moneys.get(i).getTypeid().getFyname(),
					moneys.get(i).getNumber(),
					moneys.get(i).getTypeid().getUnit(),
					moneys.get(i).getTypeid().getFyprice(),
					moneys.get(i).getStartDate(),
					moneys.get(i).getEndDate(),
					"未缴费"
			};
			}else {
				data[i]=new Object[] {
						moneys.get(i).getMid(),
						moneys.get(i).getUserid().getRealname(),
						moneys.get(i).getTypeid().getFyname(),
						moneys.get(i).getNumber(),
						moneys.get(i).getTypeid().getUnit(),
						moneys.get(i).getTypeid().getFyprice(),
						moneys.get(i).getStartDate(),
						moneys.get(i).getEndDate(),
						"已缴费"
				};
			}
			
		}
		table_1.setModel(new DefaultTableModel(
				data,
				new String[] {
					"\u7F16\u53F7", "\u4E1A\u4E3B\u59D3\u540D", "\u8D39\u7528\u7C7B\u578B", "\u7528\u91CF", "\u5355\u4EF7", "\u5E94\u7F34\u8D39\u7528", "\u8D77\u59CB\u65E5\u671F", "\u7EC8\u6B62\u65E5\u671F", "\u72B6\u6001"
				}
			));
		table_1.getColumnModel().getColumn(0).setPreferredWidth(20);
		table_1.getColumnModel().getColumn(4).setPreferredWidth(80);
		table_1.getColumnModel().getColumn(6).setPreferredWidth(90);
		table_1.getColumnModel().getColumn(7).setPreferredWidth(90);
		table_1.getColumnModel().getColumn(8).setPreferredWidth(40);
		table_1.getTableHeader().setFont(new Font("微软雅黑", 0, 20));
	}
	
	public static void update() {
		
	}
	
	
	/**
	 * Create the frame.
	 */
	public FYGL() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(FYGL.class.getResource("/com/ejia/photo/客户管理系统.png")));
		setTitle("业主物业费用管理");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 1457, 868);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(new LineBorder(new Color(255,147,0)), "\u4E1A\u4E3B\u4F4F\u6237\u4FE1\u606F", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 120, 215)));
		panel.setBounds(15, 84, 464, 407);
		contentPane.add(panel);
		panel.setLayout(null);
		
		JSpinner spinner = new JSpinner();
		spinner.setFont(new Font("宋体", Font.PLAIN, 22));
		spinner.setModel(new SpinnerListModel(new String[] {"\u4E3D\u666F\u8C6A\u5EAD", "\u65F6\u4EE3\u540D\u90FD", "\u9EC4\u91D1\u56FD\u9645", "\u4E2D\u745E\u534E\u4FA8\u57CE"}));
		spinner.setBounds(15, 38, 224, 28);
		panel.add(spinner);
		
		JButton button_5 = new JButton("查询");
		button_5.setFont(new Font("宋体", Font.PLAIN, 22));
		button_5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dataBind(""+spinner.getValue());
			}
		});
		button_5.setBounds(297, 37, 123, 29);
		panel.add(button_5);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(15, 89, 434, 303);
		panel.add(scrollPane);
		
		table = new JTable();
		dataBind("");
		table.setRowHeight(40);
		table.setFont(new Font("宋体", Font.PLAIN, 20));
		scrollPane.setViewportView(table);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new TitledBorder(new LineBorder(new Color(255, 147, 0)), "\u7269\u4E1A\u7F34\u8D39\u660E\u7EC6", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 120, 215)));
		panel_1.setBounds(494, 84, 922, 407);
		contentPane.add(panel_1);
		panel_1.setLayout(null);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(15, 31, 892, 361);
		panel_1.add(scrollPane_1);
		
		table_1 = new JTable();
		dataBind1("");
		table_1.setRowHeight(30);
		table_1.setFont(new Font("宋体", Font.PLAIN, 18));
		scrollPane_1.setViewportView(table_1);
		
		
		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new TitledBorder(new LineBorder(new Color(255, 147, 0)), "\u6DFB\u52A0\u4E1A\u4E3B\u7F34\u8D39\u8BB0\u5F55", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 120, 215)));
		panel_2.setBounds(15, 506, 1401, 291);
		contentPane.add(panel_2);
		panel_2.setLayout(null);
		
		JLabel label = new JLabel("业主姓名:      "+"              住户信息:   ");
		label.setFont(new Font("宋体", Font.PLAIN, 32));
		label.setBounds(30, 35, 1091, 49);
		panel_2.add(label);
		
		JLabel lblNewLabel = new JLabel("起始日期");
		lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 32));
		lblNewLabel.setBounds(34, 119, 135, 48);
		panel_2.add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("欠费类型");
		lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 32));
		lblNewLabel_1.setBounds(34, 202, 135, 48);
		panel_2.add(lblNewLabel_1);
		
		JLabel lblNewLabel_2 = new JLabel("欠费金额");
		lblNewLabel_2.setFont(new Font("宋体", Font.PLAIN, 32));
		lblNewLabel_2.setBounds(864, 119, 135, 48);
		panel_2.add(lblNewLabel_2);
		
		JLabel label_1 = new JLabel("终止日期");
		label_1.setFont(new Font("宋体", Font.PLAIN, 32));
		label_1.setBounds(449, 119, 135, 48);
		panel_2.add(label_1);
		
		JLabel label_2 = new JLabel("欠费用量");
		label_2.setFont(new Font("宋体", Font.PLAIN, 32));
		label_2.setBounds(449, 202, 135, 48);
		panel_2.add(label_2);
		
		JLabel label_3 = new JLabel("缴费状态");
		label_3.setFont(new Font("宋体", Font.PLAIN, 32));
		label_3.setBounds(864, 202, 135, 48);
		panel_2.add(label_3);
		
		JSpinner spinner_1 = new JSpinner();
		spinner_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseEntered(MouseEvent e) {
			
			}
		});
		spinner_1.setFont(new Font("宋体", Font.PLAIN, 28));
		fytypes=MtypeService.find();
		Object[][] data1= new Object[fytypes.size()][];
		for (int i = 0; i < data1.length; i++) {
			data1[i]=new Object[] {
					fytypes.get(i).getFyname()
			};
		}
		String[] data = new String[fytypes.size()];
		for (int i = 0; i < data1.length; i++) {
			data[i]=(String) data1[i][0];
		}
		spinner_1.setModel(new SpinnerListModel(data));
		spinner_1.setBounds(203, 202, 212, 48);
		panel_2.add(spinner_1);
		
		JSpinner spinner_2 = new JSpinner();
		spinner_2.setFont(new Font("宋体", Font.PLAIN, 28));
		spinner_2.setModel(new SpinnerListModel(new String[] {"\u5DF2\u7F34\u8D39", "\u672A\u7F34\u8D39"}));
		spinner_2.setBounds(1033, 202, 124, 48);
		panel_2.add(spinner_2);
		
		txtnumber = new JTextField();
		txtnumber.setFont(new Font("宋体", Font.PLAIN, 28));
		txtnumber.setBounds(618, 202, 212, 48);
		panel_2.add(txtnumber);
		txtnumber.setColumns(10);
		
		JLabel lblNewLabel_3 = new JLabel("未计算");
		lblNewLabel_3.setFont(new Font("宋体", Font.PLAIN, 28));
		lblNewLabel_3.setBounds(1033, 119, 124, 48);
		panel_2.add(lblNewLabel_3);
		
		txtstat = new JTextField();
		txtstat.setFont(new Font("宋体", Font.PLAIN, 28));
		txtstat.setBounds(203, 119, 212, 48);
		panel_2.add(txtstat);
		txtstat.setColumns(10);
		
		txtend = new JTextField();
		txtend.setFont(new Font("宋体", Font.PLAIN, 28));
		txtend.setColumns(10);
		txtend.setBounds(618, 119, 212, 48);
		panel_2.add(txtend);
		
		
		
		JButton btnNewButton = new JButton("计算金额");
		btnNewButton.setFont(new Font("宋体", Font.PLAIN, 32));
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int number=Integer.parseInt(txtnumber.getText());
				String name=(String) spinner_1.getValue();
				double price=MoneyService.fyprice(name);
				jsjg=price*number;
				lblNewLabel_3.setText(""+jsjg);
			}
		});
		btnNewButton.setBounds(1191, 117, 175, 52);
		panel_2.add(btnNewButton);
		
		
		JButton button_6 = new JButton("保存记录");
		button_6.setFont(new Font("宋体", Font.PLAIN, 32));
		button_6.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (table.getSelectedRowCount()>0) {
					String stat= txtstat.getText();
					int status=0;
					if (((String) spinner_2.getValue()).equals("未缴费")) {
						status=0;
					}else {
						status=1;
					}
					String end=txtend.getText();
					int number=Integer.parseInt(txtnumber.getText());
					double price=MoneyService.fyprice((String) spinner_1.getValue());
					jsjg=price*number;
					int index=table.getSelectedRow();
					String name=users.get(index).getRealname();
					String hname=users.get(index).getHid().getHname();
					fytypes=MtypeService.findid((String) spinner_1.getValue());
					int typeid=fytypes.get(0).getFyid();
					users=UserService.find(name);
					int userid=users.get(0).getId();
					if (MoneyService.add(userid, typeid, stat, end, number, status)) {
						JOptionPane.showMessageDialog(null, "添加成功");
					}else {
						JOptionPane.showMessageDialog(null, "添加失败");
					}
					dataBind1(name);
					dataBind("");
					txtend.setText("");
					txtName.setText("");
					txtnumber.setText("");
					txtstat.setText("");
					lblNewLabel_3.setText("未计算");
					label.setText("业主姓名:   "+"             住户信息:   ");
				}else {
						JOptionPane.showMessageDialog(null, "请先选择用户");
					}
				
				
			}
		});
		button_6.setBounds(1191, 200, 175, 52);
		panel_2.add(button_6);
		
		JButton button = new JButton("查看账单");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (table.getSelectedRowCount()>0) {
					int index=table.getSelectedRow();
					String name=users.get(index).getRealname();
					String hname=users.get(index).getHid().getHname();
					label.setText("业主姓名:   "+name+"             住户信息:   "+hname);
					dataBind1(name);
				}else {
					JOptionPane.showMessageDialog(null, "请先选择用户");
				}
			}
		});
		button.setFont(new Font("宋体", Font.PLAIN, 24));
		button.setIcon(new ImageIcon(FYGL.class.getResource("/com/ejia/photo/查看账单min.png")));
		button.setBounds(188, 15, 189, 54);
		contentPane.add(button);
		
		JButton button_1 = new JButton("缴费");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (table_1.getSelectedRowCount()>0) {
					int index=table_1.getSelectedRow();
					int id=moneys.get(index).getMid();
					MoneyService.update(id);
					dataBind1("");
					JOptionPane.showMessageDialog(null,"缴费成功");
				}else {
					JOptionPane.showMessageDialog(null, "请先选择用户");
				}
			}
		});
		button_1.setFont(new Font("宋体", Font.PLAIN, 24));
		button_1.setIcon(new ImageIcon(FYGL.class.getResource("/com/ejia/photo/缴费min.png")));
		button_1.setBounds(401, 15, 134, 54);
		contentPane.add(button_1);
		
		JButton button_2 = new JButton("删除");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (table_1.getSelectedRowCount()>0) {
					int index=table_1.getSelectedRow();
					int id=moneys.get(index).getMid();
					if (MoneyService.delete(id)) {
						dataBind1("");
						JOptionPane.showMessageDialog(null, "删除成功");
					}else {
						JOptionPane.showMessageDialog(null, "删除失败");
					}
				} else {
					JOptionPane.showMessageDialog(null, "请先选择用户");
				}
			}
		});
		button_2.setFont(new Font("宋体", Font.PLAIN, 24));
		button_2.setIcon(new ImageIcon(FYGL.class.getResource("/com/ejia/photo/删除min.png")));
		button_2.setBounds(559, 15, 140, 54);
		contentPane.add(button_2);
		
		JButton button_3 = new JButton("刷新");
		button_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				label.setText("业主姓名:    "+"                住户信息:   ");
				dataBind1("");
			}
		});
		button_3.setFont(new Font("宋体", Font.PLAIN, 24));
		button_3.setIcon(new ImageIcon(FYGL.class.getResource("/com/ejia/photo/刷新min.png")));
		button_3.setBounds(723, 15, 140, 54);
		contentPane.add(button_3);
		
		JButton button_4 = new JButton("搜索");
		button_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String name=txtName.getText();
				dataBind1(name);
			}
		});
		button_4.setFont(new Font("宋体", Font.PLAIN, 24));
		button_4.setIcon(new ImageIcon(FYGL.class.getResource("/com/ejia/photo/搜索min.png")));
		button_4.setBounds(1270, 15, 140, 54);
		contentPane.add(button_4);
		
		txtName = new JTextField();
		txtName.setFont(new Font("宋体", Font.PLAIN, 24));
		txtName.setBounds(1055, 22, 191, 40);
		contentPane.add(txtName);
		txtName.setColumns(10);
		
		JButton button_7 = new JButton("返回");
		button_7.setFont(new Font("宋体", Font.PLAIN, 24));
		button_7.setIcon(new ImageIcon(FYGL.class.getResource("/com/ejia/photo/返回.png")));
		button_7.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new Main().setVisible(true);
				dispose();
			}
		});
		button_7.setBounds(24, 15, 140, 54);
		contentPane.add(button_7);
		
		JLabel label_5 = new JLabel("业主姓名：");
		label_5.setFont(new Font("宋体", Font.PLAIN, 28));
		label_5.setBounds(887, 17, 144, 50);
		contentPane.add(label_5);
		
		this.setLocationRelativeTo(null);
	}
}
